package net.xiangcaowuyu.shiro.service.impl;

import net.xiangcaowuyu.shiro.entity.ShiroPermission;
import net.xiangcaowuyu.shiro.entity.ShiroRole;
import net.xiangcaowuyu.shiro.mapper.ShiroRoleMapper;
import net.xiangcaowuyu.shiro.service.ShiroPermissionService;
import net.xiangcaowuyu.shiro.service.ShiroRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @author laughing
 * @date 2020/10/11
 * @site https://www.lisen.org
 */
@Service
public class ShiroRoleServiceImpl implements ShiroRoleService {

    @Resource
    ShiroRoleMapper shiroRoleMapper;

    @Resource
    ShiroPermissionService shiroPermissionService;

    /**
     * 根据用户编号获取其角色列表
     *
     * @param userCode 用户编号
     * @return 权限列表
     */
    @Override
    public Set<ShiroRole> findByUserCode(String userCode) {
        Set<ShiroRole> shiroRoleSet = shiroRoleMapper.findByUserCode(userCode);
        for (ShiroRole shiroRole : shiroRoleSet) {
            Set<ShiroPermission> shiroPermissionSet = shiroPermissionService.findByRoleId(shiroRole.getId());
            shiroRole.setShiroPermissionSet(shiroPermissionSet);
        }
        return shiroRoleSet;
    }
}
